package com.zt.questionnaire.controller;

import com.zt.questionnaire.common.HService;
import com.zt.questionnaire.common.annotation.AccountLoginVerify;
import com.zt.questionnaire.common.annotation.validator.groups.AddGroup;
import com.zt.questionnaire.common.msg.HResultMsg;
import com.zt.questionnaire.common.utils.AssertUtils;
import com.zt.questionnaire.common.utils.JsonUtils;
import com.zt.questionnaire.db.entity.ReportOnboardingEntity;
import com.zt.questionnaire.db.entity.ReportEntity;
import com.zt.questionnaire.db.entity.UserEntity;
import com.zt.questionnaire.service.ReportOnboardingService;
import com.zt.questionnaire.service.ReportService;
import com.zt.questionnaire.service.UserService;
import com.zt.questionnaire.service.excel.HExcelQuestionnaireService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @Description: 报告，不对应数据库表
 * @Author: funnywus
 * @Date: 2023/9/4 14:14
 */
@Validated
@Slf4j
@RestController
@RequestMapping(value = "/report", produces = "application/json; charset=UTF-8")
public class ReportController {
    @Autowired
    private HExcelQuestionnaireService excelQuestionnaireService;
    @Autowired
    private ReportService service;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ReportOnboardingService reportOnboardingService;
    @Autowired
    private UserService userService;

    /**
     * 获取问卷的问题
     */
    @AccountLoginVerify
    @GetMapping(value = "/question/get", produces = "application/json; charset=UTF-8")
    public String getQuestion(@NotNull(message = "type is null") Integer type) {
        AssertUtils.isFalse(HExcelQuestionnaireService.QUESTIONNAIRE_TYPE_LIST.contains(type), "问卷不存在");

        Map<String, Object> questionAndOption = excelQuestionnaireService.getQuestionAndOption(type);
        return HResultMsg.createMsgSucc(questionAndOption);
    }

    /**
     * 保存报告
     */
    @AccountLoginVerify
    @PostMapping(value = "/save", produces = "application/json; charset=UTF-8")
    public String save(@Validated(AddGroup.class) @RequestBody ReportEntity obj) {
        log.info("保存报告={}", JsonUtils.toString(obj));
        Long accountId = HService.getAccountId(request);
        boolean is = service.save(obj, accountId);
        AssertUtils.isFalse(is, "保存报告失败");
        return HResultMsg.createMsgSucc();
    }

    /**
     * 获取报告，根据用户id+问卷类型 来获取当前用户有没有回答过此问卷，有返回，无返回空
     */
    @AccountLoginVerify
    @GetMapping(value = "/get/by/type", produces = "application/json; charset=UTF-8")
    public String getByType(@NotNull(message = "type is null") Integer type) {
        Long accountId = HService.getAccountId(request);
        ReportEntity report = service.getByType(accountId, type);
        return HResultMsg.createMsgSucc(report);
    }

    /**
     * 获取报告
     */
    @GetMapping(value = "/get", produces = "application/json; charset=UTF-8")
    public String get(@NotNull(message = "id is null") Long id) {
        ReportEntity report = service.get(id);
        if (report == null) {
            return HResultMsg.createMsgSucc();
        }
        Long userId = report.getUserId();
        UserEntity user = userService.get(userId);
        if (user != null) {
            report.setAvatarUrl(user.getAvatarUrl());
        }
        Integer type = report.getType();
        if (HExcelQuestionnaireService.QUESTIONNAIRE_TYPE_ONBORADING == type) {
            ReportOnboardingEntity onboardingReport = reportOnboardingService.get(report.getId());
            report.setExtend(onboardingReport);
        }
        return HResultMsg.createMsgSucc(report);
    }
}
